package com.tinama.jedis01;

import java.util.concurrent.RecursiveTask;

/**
 * @author lgc
 * @createDate 2020/10/16 - 14:32
 */
public class ForkJoinTest extends RecursiveTask<Long> {

    private Long start;
    private Long end;

    private Long temp = 1000L;

    public ForkJoinTest(Long start, Long end) {
        this.start = start;
        this.end = end;
    }

    @Override
    protected Long compute() {
        if ((end-start)<temp){
            Long sum = 0L;
            for (Long i = start; i <= end; i++) {
                sum+=i;
            }
            return sum;
        }else {
            long middle= (start+end)/2;
            ForkJoinTest test1 = new ForkJoinTest(start, middle);
            test1.fork();
            ForkJoinTest test2 = new ForkJoinTest(middle, end);
            test2.fork();
            return test1.join() + test2.join();
        }
    }

    public static void main(String[] args){
        Long sum = 0L;
        long start = System.currentTimeMillis();
        for (Long i = 1L; i <= 1_0000_0000; i++) {
            sum += i;
        }
        long end = System.currentTimeMillis();
        System.out.println("sum="+sum+" 时间："+(end-start));
    }
}
